/** * AJAX Search Script for Personnel Certification * Displays certification fields in table format */ (function($) { 'use strict'; $(document).ready(function() { // Handle search form submission $('#pc-search-form').on('submit', function(e) { e.preventDefault(); var searchQuery = $('#pc-search-input').val().trim(); if (searchQuery === '') { alert('Please enter a search term'); return; } // Show preloader $('#pc-preloader').fadeIn(); $('#pc-results-container').hide(); $('#pc-no-results').hide(); // AJAX request $.ajax({ url: pcAjax.ajax_url, type: 'POST', data: { action: 'pc_search_certification', nonce: pcAjax.nonce, search_query: searchQuery }, success: function(response) { // Hide preloader $('#pc-preloader').fadeOut(); if (response.success) { displayResults(response.data.results, response.data.count); } else { showNoResults(response.data.message); } }, error: function() { $('#pc-preloader').fadeOut(); alert('An error occurred. Please try again.'); } }); }); /** * Display search results in comprehensive table format */ function displayResults(results, count) { // Build results counter var counterHtml = '
| Client ID | '; tableHtml += 'Name | '; tableHtml += 'Certification Scope | '; tableHtml += 'Certification No | '; tableHtml += 'Original Date | '; tableHtml += 'Decision Date | '; tableHtml += 'Expiry Date | '; tableHtml += '|
|---|---|---|---|---|---|---|---|
| '; tableHtml += '' + escapeHtml(item.client_id) + ''; tableHtml += ' | '; // Name tableHtml += '';
tableHtml += ' ' + escapeHtml(item.name) + ' ';
tableHtml += ' | ';
// Certification Scheme
tableHtml += ''; if (item.certification_scheme) { tableHtml += '' + escapeHtml(item.certification_scheme) + ''; } else { tableHtml += '—'; } tableHtml += ' | '; // Certification No tableHtml += ''; tableHtml += '' + escapeHtml(item.certification_no) + ''; tableHtml += ' | '; // Original Date tableHtml += ''; tableHtml += '' + (item.original_date ? escapeHtml(item.original_date) : '—') + ''; tableHtml += ' | '; // Decision Date tableHtml += ''; tableHtml += '' + (item.decision_date ? escapeHtml(item.decision_date) : '—') + ''; tableHtml += ' | '; // Expiry Date /*tableHtml += ''; if (item.expiry_date) { tableHtml += ''; tableHtml += escapeHtml(item.expiry_date); tableHtml += ''; } else { tableHtml += '—'; } tableHtml += ' | ';*/ tableHtml += ''; tableHtml += '' + (item.expiry_date ? escapeHtml(item.expiry_date) : '—') + ''; tableHtml += ' | '; tableHtml += '
' + (message || 'Try a different search term') + '
'; $('#pc-no-results').html(html).fadeIn(); } /** * Escape HTML to prevent XSS */ function escapeHtml(text) { if (!text) return ''; var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return String(text).replace(/[&<>"']/g, function(m) { return map[m]; }); } /** * Clear search results */ $('#pc-clear-search').on('click', function(e) { e.preventDefault(); $('#pc-search-input').val(''); $('#pc-results-container').empty().hide(); $('#pc-no-results').hide(); }); }); })(jQuery);